package controllers

import (
	"LoveU/models"
	"LoveU/utils"
	"strconv"

	"github.com/astaxie/beego"
)

type Usercontroller struct {
	beego.Controller
}

// 完善用户信息
func (this *Usercontroller) PerfectInfor() {
	sid := this.Input().Get("sessionid")
	mySession := SessionStore[sid]
	if mySession == nil {
		notLoginUser(this)
		return
	}
	userid := mySession.Get("userid")
	gentle := this.Input().Get("gentle")
	birthday := this.Input().Get("birthday")
	job := this.Input().Get("job")

	province := this.Input().Get("province")
	country := this.Input().Get("country")
	county := this.Input().Get("county")
	areaid, code := models.GetAreaId(province, country, county)
	if code != utils.AREA_CODE_OK {
		getJSONUser(this, code, "", "", nil)
		return
	}

	salary := this.Input().Get("salary")
	havekids := this.Input().Get("havekids")
	parentsalive := this.Input().Get("parentsalive")
	maritallstatus := this.Input().Get("maritallstatus")
	emotion := this.Input().Get("emotion")
	hobby := this.Input().Get("hobby")
	requir := this.Input().Get("requir")
	avatar := this.Input().Get("avatar")
	status := models.Register2(userid.(int64), areaid, gentle, birthday, job, salary, havekids, parentsalive, maritallstatus, emotion, hobby, requir, avatar)
	switch status {
	case utils.ARGUMENT_DENIED:
		getJSONUser(this, utils.ARGUMENT_DENIED, "参数错误", "", nil)
		return
	case utils.UNKNOWN_ERROR:
		getJSONUser(this, utils.UNKNOWN_ERROR, "系统错误", "", nil)
		return
	case utils.OK:
		getJSONUser(this, utils.OK, "success", "", nil)
		return
	}
	return
}

// 返回用户个人信息
func (this *Usercontroller) GetUserInfo() {
	sid := this.Input().Get("sessionid")
	mySession := SessionStore[sid]
	if mySession == nil {
		notLoginUser(this)
		return
	}
	myUserId := mySession.Get("userid")
	extraUserId := this.Input().Get("id")
	extraUserName := this.Input().Get("name")
	var targetId int64
	if extraUserId == "" && extraUserName == "" {
		targetId = myUserId.(int64)
	} else if extraUserId == "" {
		users := models.GetUsersByName(extraUserName)
		if users == nil || len(users) != 1 {
			getJSONUser(this, utils.USER_NOT_FOUND, "不存在该用户，或信息冗余", "", nil)
			return
		}
		targetId = users[0].Id
	} else if extraUserName == "" {
		temp, err := strconv.ParseInt(extraUserId, 10, 64)
		if err != nil {
			getJSONUser(this, utils.ARGUMENT_DENIED, "参数错误(id)", "", nil)
			return
		}
		targetId = temp
	} else {
		getJSONUser(this, utils.ARGUMENT_DENIED, "参数错误(id,name)", "", nil)
		return
	}
	user := models.GetUserInfo(targetId)
	if user == nil {
		getJSONUser(this, utils.UNKNOWN_ERROR, "未知错误", "", nil)
		return
	}
	getJSONUser(this, utils.OK, "success", "", user)
	return
}

//用户提交VIP申请
func (this *Usercontroller) RequestVIP() {
	sid := this.Input().Get("sessionid")
	mySession := SessionStore[sid]
	if mySession == nil {
		notLoginUser(this)
		return
	}
	userid := mySession.Get("userid")

	url1 := this.Input().Get("url1")
	url2 := this.Input().Get("url2")
	url3 := this.Input().Get("url3")
	vipStr, status := models.RequestVIP(userid.(int64), url1, url2, url3)
	switch status {
	case utils.UNKNOWN_ERROR:
		getJSONUser(this, status, vipStr, "", nil)
		return
	case utils.VIP_REJECT:
		getJSONUser(this, status, vipStr, "", nil)
		return
	case utils.VIP_REQUEST_SENDED:
		getJSONUser(this, status, vipStr, "", nil)
		return
	case utils.VIP_USERR:
		getJSONUser(this, status, vipStr, "", nil)
		return
	case utils.OK:
		getJSONUser(this, status, vipStr, "", nil)
		return
	default:
		getJSONUser(this, utils.UNKNOWN_ERROR, "未知错误", "", nil)
		return
	}

}

func (this *Usercontroller) CheckVIP() {
	sid := this.Input().Get("sessionid")
	mySession := SessionStore[sid]
	if mySession == nil {
		notLoginUser(this)
		return
	}
	userid := mySession.Get("userid")
	vipStr, status := models.CheckVIP(userid.(int64))
	switch status {
	case utils.UNKNOWN_ERROR:
		getJSONUser(this, status, vipStr, "", nil)
		return
	case utils.VIP_REJECT:
		getJSONUser(this, status, vipStr, "", nil)
		return
	case utils.VIP_REQUEST_SENDED:
		getJSONUser(this, status, vipStr, "", nil)
		return
	case utils.VIP_USERR:
		getJSONUser(this, status, vipStr, "", nil)
		return
	case utils.OK:
		getJSONUser(this, status, vipStr, "", nil)
		return
	default:
		getJSONUser(this, utils.UNKNOWN_ERROR, "未知错误", "", nil)
		return
	}
}

// 修改兴趣爱好
func (this *Usercontroller) ModifyHobby() {
	sid := this.Input().Get("sessionid")
	mySession := SessionStore[sid]
	if mySession == nil {
		notLoginUser(this)
		return
	}
	myUserId := mySession.Get("userid")
	hobby := this.Input().Get("hobby")
	flag := models.ModifyHobby(myUserId.(int64), hobby)
	if flag == false {
		getJSONUser(this, utils.UNKNOWN_ERROR, "未知错误", "", nil)
		return
	}
	getJSONUser(this, utils.OK, "success", "", nil)
	return
}

//修改签名
func (this *Usercontroller) ModifyAutograph() {
	sid := this.Input().Get("sessionid")
	mySession := SessionStore[sid]
	if mySession == nil {
		notLoginUser(this)
		return
	}
	myUserId := mySession.Get("userid")
	autograph := this.Input().Get("autograph")
	flag := models.ModifyAutograph(myUserId.(int64), autograph)
	if flag == false {
		getJSONUser(this, utils.UNKNOWN_ERROR, "未知错误", "", nil)
		return
	}
	getJSONUser(this, utils.OK, "success", "", nil)
	return
}

// 修改性别
func (this *Usercontroller) ModifyGentle() {
	sid := this.Input().Get("sessionid")
	mySession := SessionStore[sid]
	if mySession == nil {
		notLoginUser(this)
		return
	}
	myUserId := mySession.Get("userid")
	gentle := this.Input().Get("gentle")
	gentle_int64, err := strconv.ParseInt(gentle, 10, 64)
	if err != nil {
		getJSONUser(this, utils.UNKNOWN_ERROR, "格式错误", "", nil)
		return
	}
	flag := models.ModifyGentle(myUserId.(int64), gentle_int64)
	if flag == false {
		getJSONUser(this, utils.UNKNOWN_ERROR, "未知错误", "", nil)
		return
	}
	getJSONUser(this, utils.OK, "success", "", nil)
	return
}

//修改生日
func (this *Usercontroller) ModifyBirthday() {
	sid := this.Input().Get("sessionid")
	mySession := SessionStore[sid]
	if mySession == nil {
		notLoginUser(this)
		return
	}
	myUserId := mySession.Get("userid")
	birthday := this.Input().Get("birthday")
	birthday_date, err := String2Time(birthday)
	if err != nil {
		getJSONUser(this, utils.UNKNOWN_ERROR, "格式错误", "", nil)
		return
	}
	flag := models.ModifyBirthday(myUserId.(int64), birthday_date)
	if flag == false {
		getJSONUser(this, utils.UNKNOWN_ERROR, "未知错误", "", nil)
		return
	}
	getJSONUser(this, utils.OK, "success", "", nil)
	return
}

//修改地域
func (this *Usercontroller) ModifyArea() {
	sid := this.Input().Get("sessionid")
	mySession := SessionStore[sid]
	if mySession == nil {
		notLoginUser(this)
		return
	}
	myUserId := mySession.Get("userid")
	area := this.Input().Get("area")
	area_int64, err := strconv.ParseInt(area, 10, 64)
	if err != nil {
		getJSONUser(this, utils.UNKNOWN_ERROR, "格式错误", "", nil)
		return
	}
	flag := models.ModifyArea(myUserId.(int64), area_int64)
	if flag == false {
		getJSONUser(this, utils.UNKNOWN_ERROR, "未知错误", "", nil)
		return
	}
	getJSONUser(this, utils.OK, "success", "", nil)
	return
}

// 修改职业
func (this *Usercontroller) ModifyJob() {
	sid := this.Input().Get("sessionid")
	mySession := SessionStore[sid]
	if mySession == nil {
		notLoginUser(this)
		return
	}
	myUserId := mySession.Get("userid")
	job := this.Input().Get("job")
	flag := models.ModifyJob(myUserId.(int64), job)
	if flag == false {
		getJSONUser(this, utils.UNKNOWN_ERROR, "未知错误", "", nil)
		return
	}
	getJSONUser(this, utils.OK, "success", "", nil)
	return
}

// 修改工资
func (this *Usercontroller) ModifySalary() {
	sid := this.Input().Get("sessionid")
	mySession := SessionStore[sid]
	if mySession == nil {
		notLoginUser(this)
		return
	}
	myUserId := mySession.Get("userid")
	salary := this.Input().Get("salary")

	salary_float, err := strconv.ParseFloat(salary, 64)
	if err != nil {
		getJSONUser(this, utils.UNKNOWN_ERROR, "格式错误", "", nil)
		return
	}
	flag := models.ModifySalary(myUserId.(int64), salary_float)
	if flag == false {
		getJSONUser(this, utils.UNKNOWN_ERROR, "未知错误", "", nil)
		return
	}
	getJSONUser(this, utils.OK, "success", "", nil)
	return
}

// 是否有孩子
func (this *Usercontroller) ModifyHaveKids() {
	sid := this.Input().Get("sessionid")
	mySession := SessionStore[sid]
	if mySession == nil {
		notLoginUser(this)
		return
	}
	myUserId := mySession.Get("userid")
	havekids := this.Input().Get("havekids")
	if havekids == "1" || havekids == "2" {

	} else {
		getJSONUser(this, utils.UNKNOWN_ERROR, "未知错误", "", nil)
		return
	}
	var status bool
	if havekids == "1" {
		status = true
	} else {
		status = false
	}
	flag := models.ModifyHaveKids(myUserId.(int64), status)
	if flag == false {
		getJSONUser(this, utils.UNKNOWN_ERROR, "未知错误", "", nil)
		return
	}
	getJSONUser(this, utils.OK, "success", "", nil)
	return
}

// 父母是否健在
func (this *Usercontroller) ModifyParentsalive() {
	sid := this.Input().Get("sessionid")
	mySession := SessionStore[sid]
	if mySession == nil {
		notLoginUser(this)
		return
	}
	myUserId := mySession.Get("userid")
	parentsalive := this.Input().Get("parentsalive")
	if parentsalive != "1" && parentsalive != "2" {
		getJSONUser(this, utils.UNKNOWN_ERROR, "未知错误", "", nil)
		return
	}
	var status bool
	if parentsalive == "1" {
		status = true
	} else {
		status = false
	}
	flag := models.ModifyParentsalive(myUserId.(int64), status)
	if flag == false {
		getJSONUser(this, utils.UNKNOWN_ERROR, "未知错误", "", nil)
		return
	}
	getJSONUser(this, utils.OK, "success", "", nil)
	return
}

// 情感状况
func (this *Usercontroller) ModifyEmotion() {
	sid := this.Input().Get("sessionid")
	mySession := SessionStore[sid]
	if mySession == nil {
		notLoginUser(this)
		return
	}
	myUserId := mySession.Get("userid")
	emtion := this.Input().Get("emotion")
	flag := models.ModifyEmotion(myUserId.(int64), emtion)
	if flag == false {
		getJSONUser(this, utils.UNKNOWN_ERROR, "未知错误", "", nil)
		return
	}
	getJSONUser(this, utils.OK, "success", "", nil)
	return
}

//婚姻状况
func (this *Usercontroller) ModifyMaritallstatus() {
	sid := this.Input().Get("sessionid")
	mySession := SessionStore[sid]
	if mySession == nil {
		notLoginUser(this)
		return
	}
	myUserId := mySession.Get("userid")
	maritallstatus := this.Input().Get("maritallstatus")
	flag := models.ModifyMaritallstatus(myUserId.(int64), maritallstatus)
	if flag == false {
		getJSONUser(this, utils.UNKNOWN_ERROR, "未知错误", "", nil)
		return
	}
	getJSONUser(this, utils.OK, "success", "", nil)
	return
}

// 修改需求
func (this *Usercontroller) ModifyRequir() {
	sid := this.Input().Get("sessionid")
	mySession := SessionStore[sid]
	if mySession == nil {
		notLoginUser(this)
		return
	}
	myUserId := mySession.Get("userid")
	requir := this.Input().Get("requir")
	flag := models.ModifyRequir(myUserId.(int64), requir)
	if flag == false {
		getJSONUser(this, utils.UNKNOWN_ERROR, "未知错误", "", nil)
		return
	}
	getJSONUser(this, utils.OK, "success", "", nil)
	return
}
